<?php 
class SearchPlace_model extends Model{

	function __construct (){
		parent::__construct();
	}

	public function get_product_search_by_key($key) {
		$sql = " SELECT *  FROM place where MATCH(name,address,steet,city) AGAINST(:key)";
		$_array = array(':key' => $key );
		
		if($key == ""){
			$sql = " SELECT *  FROM place ";
			$_array =array();
		}
		$q = $this->db->prepare($sql);
		$q->execute($_array);
		$data = $q->fetchAll();
		return $data;
	}
	
	public function get_all_wifi($place_id){
		$q = $this->db->prepare("SELECT id,name,password,votes,(votes+unvotes) as total,
			CASE 
				WHEN ( votes + unvotes ) > 0 THEN ROUND( ( votes / ( votes + unvotes ) ) * 100 )
			ELSE 0 END as rate ,pin,
			CASE 
				WHEN TIMESTAMPDIFF(YEAR,last_date,NOW()) > 0 
					THEN TIMESTAMPDIFF(YEAR,last_date,NOW())
				WHEN TIMESTAMPDIFF(MONTH,last_date,NOW()) > 0 
					THEN TIMESTAMPDIFF(MONTH,last_date,NOW())
				WHEN TIMESTAMPDIFF(DAY,last_date,NOW()) > 0 
					THEN TIMESTAMPDIFF(DAY,last_date,NOW())
				WHEN TIMESTAMPDIFF(HOUR,last_date,NOW()) > 0 
					THEN TIMESTAMPDIFF(HOUR,last_date,NOW())
				WHEN TIMESTAMPDIFF(MINUTE,last_date,NOW()) > 0 
					THEN TIMESTAMPDIFF(MINUTE,last_date,NOW())
				WHEN TIMESTAMPDIFF(SECOND,last_date,NOW()) > 0 
					THEN TIMESTAMPDIFF(SECOND,last_date,NOW())
				ELSE ' ' END AS last_date,
			CASE 
				WHEN TIMESTAMPDIFF(YEAR,last_date,NOW()) > 0 
					THEN ' year ago'
				WHEN TIMESTAMPDIFF(MONTH,last_date,NOW()) > 0 
					THEN ' month ago'
				WHEN TIMESTAMPDIFF(DAY,last_date,NOW()) > 0 
					THEN ' day ago'
				WHEN TIMESTAMPDIFF(HOUR,last_date,NOW()) > 0 
					THEN ' hour ago'
				WHEN TIMESTAMPDIFF(MINUTE,last_date,NOW()) > 0 
					THEN ' minute ago'
				WHEN TIMESTAMPDIFF(SECOND,last_date,NOW()) > 0 
					THEN ' second ago'
				ELSE '' END AS title_last_date
			 FROM wifi WHERE place_id=:place_id ORDER BY name ");

		$q->execute(array(
			':place_id' => $place_id 
		));

		$data = false;
		
		try {
			$data = $q->fetchAll();
		} catch (Exception $e) {
			$data = false;
		}
		return $data;
	}
}
?>